package com.chaodev.base.controller;

import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageInfo;
import com.chaodev.base.base.RedisDao;
import com.chaodev.base.dto.LearningParkDto;
import com.chaodev.base.em.BizTypeEnum;
import com.chaodev.base.model.LearningPark;
import com.chaodev.base.model.Result;
import com.chaodev.base.model.UserInfo;
import com.chaodev.base.service.ILogService;
import com.chaodev.base.service.LearningParkService;
import com.chaodev.base.utils.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 学习园区 前端控制器
 * </p>
 *
 * @author ''
 * @since 2023-06-09
 */
@RestController
@RequestMapping("park")
@Api(value = "")
public class LearningParkController {
    @Autowired
    private RedisDao redisDao;

    @Autowired
    private ILogService logService;

    @Autowired
    LearningParkService parkService;
    /**
     * 获取学习园区列表分页信息
     * @author ''
     */
    @ApiOperation(value = "获取学习园区列表分页", notes = "获取学习园区列表分页信息")
    @PostMapping(value = {"list"})
    public Result list(@RequestBody LearningParkDto dto, HttpServletRequest request) {
        Result result = new Result();
        String token = request.getHeader("token");
        UserInfo userInfo = null;
        if (redisDao.existsKey(token)) {
            userInfo = JwtUtil.getUserInfoFromToken(token, redisDao);
            int page=dto.getPage();
            int rows=dto.getRows();
            LearningPark park = new LearningPark();
            park.setContent(dto.getContent());
            park.setTitle(dto.getTitle());
            PageInfo<LearningPark> activityNot = parkService.queryPageList(page,rows,park);
            result.setCode(0);
            result.setMsg("获取成功");
            result.setData(activityNot);
            logService.logAdd(null, "获取学习园区列表分页", "SUCCESS", result.toString(), userInfo, request);
        } else {
            result.setCode(-1);
            result.setMsg("登录超时");
            logService.logAdd(null, "获取学习园区列表分页", "ERROR", result.toString(), userInfo, request);
        }
        return result;
    }

    /**
     * 获取学习园区信息
     * @author ''
     */
    @ApiOperation(value = "获取学习园区对象", notes = "获取学习园区信息")
    @GetMapping(value = {"/getById"})
    public Result findById(String id,HttpServletRequest request) {
        Result result = new Result();
        String token = request.getHeader("token");
        UserInfo userInfo = null;
        if (redisDao.existsKey(token)) {
            userInfo = JwtUtil.getUserInfoFromToken(token, redisDao);
            LearningPark dataById = parkService.findDataById(id);
            parkService.updateVisitNum(dataById);
            result.setCode(0);
            result.setData(dataById);
            logService.logAdd(null, "获取学习园区对象", "SUCCESS", result.toString(), userInfo, request);
        } else {
            result.setCode(-1);
            result.setMsg("登录超时");
            logService.logAdd(null, "获取学习园区对象", "ERROR", result.toString(), userInfo, request);
        }
        return result;
    }

    /**
     * 保存学习园区信息
     * @author ''
     */
    @ApiOperation(value = "保存学习园区对象", notes = "保存学习园区信息")
    @PostMapping(value = {"/save"})
    public Result save(@RequestBody LearningPark learningPark,HttpServletRequest request) {
        Result result = new Result();
        String token = request.getHeader("token");
        UserInfo userInfo = null;
        if (redisDao.existsKey(token)) {
            userInfo = JwtUtil.getUserInfoFromToken(token, redisDao);
            if(StrUtil.isEmpty(learningPark.getSendUser())){
                learningPark.setSendUser(userInfo.getUserName());
            }
            if(StrUtil.isEmpty(learningPark.getBizType())){
                learningPark.setBizType(BizTypeEnum.Em_LearnPark.getValue());
            }
            if(StrUtil.isEmpty(learningPark.getcUser())){
                learningPark.setcUser(userInfo.getUserId());
            }
            if(StrUtil.isEmpty(learningPark.getSendOrgCode())){
                learningPark.setSendOrgCode(userInfo.getDepartId());
            }
            if(StrUtil.isEmpty(learningPark.getSendOrgName())){
                learningPark.setSendOrgName(userInfo.getDepartName());
            }
            if(StrUtil.isEmpty(learningPark.getUpdtr())){
                learningPark.setUpdtr(userInfo.getUserId());
            }
            parkService.saveData(learningPark,userInfo);
            result.setCode(0);
            result.setMsg("操作成功");
            result.setData(learningPark);
            logService.logAdd(null, "保存学习园区对象", "SUCCESS", result.toString(), userInfo, request);
        } else {
            result.setCode(-1);
            result.setMsg("登录超时");
            logService.logAdd(null, "保存学习园区对象", "ERROR", result.toString(), userInfo, request);
        }
        return result;
    }

    /**
     * 删除学习园区信息
     * @author ''
     */
    @ApiOperation(value = "删除学习园区对象", notes = "删除学习园区信息")
    @GetMapping(value = {"/delete"})
    public Result delete(String id, HttpServletRequest request) {
        Result result = new Result();
        String token = request.getHeader("token");
        UserInfo userInfo = null;
        if (redisDao.existsKey(token)) {
            userInfo = JwtUtil.getUserInfoFromToken(token, redisDao);
            parkService.deleteDataById(id);
            result.setCode(0);
            logService.logAdd(null, "删除学习园区对象", "SUCCESS", result.toString(), userInfo, request);
        } else {
            result.setCode(-1);
            result.setMsg("登录超时");
            logService.logAdd(null, "删除学习园区对象", "ERROR", result.toString(), userInfo, request);
        }
        return result;
    }
}
